// 模式温度设置
//  ZYModeSettingView.m
//  YiLin
//
//  Created by Zyark on 2018/6/5.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYModeSettingView.h"
#import "ZYUtils.h"
#import "ZYModeTypeModel.h"
#import "ZYImageUtils.h"
#import "ZYCustomSlider.h"

@interface ZYModeSettingView()

/**
 模式图片
 */
@property(nonatomic,strong) UIImageView *modeTypeImageView;

/**
 模式名称
 */
@property(nonatomic,strong) UILabel *modeNameLabel;

/**
 温度
 */
@property(nonatomic,strong) UILabel *temperatureLabel;

/**
 系统温度
 */
@property(nonatomic,strong) UILabel *systemLabel;


/**
 系统温度指示图片
 */
@property(nonatomic,strong) UIImageView *systemImageView;


/**
 温度进度 颜色渐变
 */
@property(nonatomic,strong) ZYCustomSlider *temperatureSlider;


@end

@implementation ZYModeSettingView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    _maxTemperature = 31;
    _systemTemperature = 26.5;
    
    CGFloat imageWidth = self.width * 0.13;
    //模式图片
    _modeTypeImageView = [[UIImageView alloc] init];
    _modeTypeImageView.size = CGSizeMake(imageWidth, imageWidth);
    [self addSubview:_modeTypeImageView];
    _modeTypeImageView.left = 17;
    _modeTypeImageView.top = imageWidth * 0.3;
    
    //模式名称
    _modeNameLabel = [[UILabel alloc] init];
    _modeNameLabel.textColor = kMainColor;
    _modeNameLabel.font = [UIFont systemFontOfSize:14];
    _modeNameLabel.size = CGSizeMake(imageWidth * 3, imageWidth  * 0.4);
    [self addSubview:_modeNameLabel];
    _modeNameLabel.top = _modeTypeImageView.top;
    _modeNameLabel.left = _modeTypeImageView.right + 20;
    
    //温度进度 颜色渐变
    CGFloat progressWidth = self.width - _modeTypeImageView.right - 30;
    _temperatureSlider = [[ZYCustomSlider alloc] initWithFrame:CGRectMake(0, 0, progressWidth, imageWidth * 0.4) withTrackHeight: imageWidth * 0.1];
    [self addSubview:_temperatureSlider];
    _temperatureSlider.left = _modeTypeImageView.right + 15;
    _temperatureSlider.bottom = _modeTypeImageView.bottom - 5;
    UIColor *startColor = [UIColor colorWithHexString:kMainColorEndColorHexString];
    UIColor *endColor = [UIColor colorWithHexString:kMainColorStartColorHexString];
    NSArray *colors = @[startColor,endColor];
    UIImage *img = [ZYUtils createImageWithSize:_temperatureSlider.size gradientColors:colors percentage:@[@0.4,@1] gradientType:ZYGradientFromLeftToRight];
    [_temperatureSlider setMinimumTrackImage:img forState:UIControlStateNormal];
    [_temperatureSlider setMaximumTrackTintColor:RGBA(235, 235, 235, 1)];
    [_temperatureSlider setMinimumValue: 10.0f];
    [_temperatureSlider setMaximumValue:_maxTemperature];
    CGRect rect = CGRectMake(0, 0, imageWidth * 0.4, imageWidth * 0.4);
    UIImage *image = [ZYImageUtils imageWithCornerRadius:rect.size.width / 2 image:[ZYImageUtils imageWithColor:kMainColor rect:rect]];
    [_temperatureSlider setThumbImage:image forState:UIControlStateNormal];
    [_temperatureSlider setContinuous:NO];//滑动结束通知
    [_temperatureSlider addTarget:self
                      action:@selector(templageChange:)
            forControlEvents:UIControlEventValueChanged];
 

    //温度
    _temperatureLabel = [[UILabel alloc] init];
    _temperatureLabel.size = CGSizeMake(100, imageWidth * 0.4);
    _temperatureLabel.textColor = RGBA(153, 153, 153, 1);
    _temperatureLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:_temperatureLabel];
    _temperatureLabel.right = self.width - 30;
    _temperatureLabel.centerY= _modeNameLabel.centerY;
    
    _systemImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_quick_eqtup_system"]];
    [self addSubview:_systemImageView];
    _systemImageView.top = _temperatureSlider.bottom - 10;
    _systemImageView.centerX = _temperatureSlider.left + ((_systemTemperature-10)/(_maxTemperature - 10)) * _temperatureSlider.width;
    
    _systemLabel = [[UILabel alloc] init];
    _systemLabel.size = CGSizeMake(100, imageWidth * 0.4);
    _systemLabel.textColor = RGBA(153, 153, 153, 1);
    _systemLabel.font = [UIFont systemFontOfSize:12];
    _systemLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_systemLabel];
    _systemLabel.centerX = _systemImageView.centerX;
    _systemLabel.top = _systemImageView.bottom + 5;
}

-(void)setMaxTemperature:(CGFloat)maxTemperature{
    _maxTemperature = maxTemperature;
}

-(void)setSystemTemperature:(CGFloat)systemTemperature{
    if(systemTemperature == 0){
        return;
    }
    _systemTemperature = systemTemperature;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    NSString *system = [language getStringByKey:LC_QUICK_SETUP_SETTING_SYSTEM];
    _systemLabel.text = [NSString stringWithFormat:@"%@%.1f℃",system,_systemTemperature];
}

-(void)updateView{
    ZYWeakSelf(weakSelf)
    [UIView animateWithDuration:0.3 animations:^{
        _systemImageView.centerX = _temperatureSlider.left + ((_systemTemperature-10)/(_maxTemperature - 10)) * _temperatureSlider.width;
        weakSelf.systemLabel.centerX = weakSelf.systemImageView.centerX;
    }];
}

-(void)setModel:(ZYModeTypeModel *)model{
    _model = model;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _modeTypeImageView.image = [UIImage imageNamed:model.imageName];
    _modeNameLabel.text = [language getStringByKey:model.title];
    NSString *system = [language getStringByKey:LC_QUICK_SETUP_SETTING_SYSTEM];
    _systemLabel.text = [NSString stringWithFormat:@"%@%.1f℃",system,_systemTemperature];
}

-(void)setCurrentTemperature:(CGFloat)currentTemperature{
    _currentTemperature = currentTemperature;
    _temperatureLabel.text = [NSString stringWithFormat:@"%.1f℃",currentTemperature];
    [_temperatureSlider setValue:currentTemperature animated:YES];
}

#pragma mark - event
-(void)templageChange:(UISlider *)slider{
    _temperatureLabel.text = [NSString stringWithFormat:@"%.1f℃",slider.value];
    _currentTemperature = slider.value;
    if([_delegate respondsToSelector:@selector(modeSettingView:changeTemperature:modeType:)]){
        [_delegate modeSettingView:self changeTemperature:slider.value modeType:_model.modeType];
    }
}

-(void)setIsNotEditable:(BOOL)isNotEditable{
    _isNotEditable = isNotEditable;
    _temperatureSlider.enabled = isNotEditable;
}
@end
